using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TDFlowLibrary;
using TDBaseDataLibrary;
namespace MapEdit
{
    public class AniCursor
    {
        int m_nCurCursor;
        double m_ndtimeescap;
        Texture2D[] m_Cursor;
        Point m_hot;
        
        public AniCursor()
        {
        }
        public void SetAniInfo(ContentManager Content,string[] CurName,Point ptHot)
        {
            int nCount = CurName.GetLength(0);
            m_Cursor = new Texture2D[nCount];
            for (int i = 0; i < nCount; i++)
            {
                m_Cursor[i] = Content.Load<Texture2D>(CurName[i]);
            }
            m_hot = ptHot;
            
        }
        public int Update(GraphicsDeviceManager graphics, GameTime gameTime)
        {
             //= gameTime.TotalGameTime.TotalMilliseconds;
            m_ndtimeescap += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (m_ndtimeescap > 200)
            {
                m_ndtimeescap = m_ndtimeescap - 200;
                m_nCurCursor = (m_nCurCursor + 1) % m_Cursor.GetLength(0);
            }
                
            return 1;

        }
        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch,int nCenterX,int nCenterY)
        {
            Rectangle rtDes;

            rtDes.X = nCenterX - m_hot.X;
            rtDes.Y = nCenterY - m_hot.Y;
            rtDes.Width = m_Cursor[m_nCurCursor].Width;
            rtDes.Height = m_Cursor[m_nCurCursor].Height;
            spriteBatch.Draw(m_Cursor[m_nCurCursor], rtDes, Color.White);
        }
    }
    public class MapControlPal:MainMenu
    {
        public MapControlPal(Game game)
            : base(game)
        {
        }
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }
        public override int Update(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            return 1;
            //return base.Update(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
        public override void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int xOffset, int yOffset, float fscale)
        {
            //spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            //m_Map.Draw(graphics, gameTime, spriteBatch, 0, 0, 1);
            //MouseState mstate = Mouse.GetState();
            //m_AniCursor.Draw(graphics, gameTime, spriteBatch, mstate.X, mstate.Y);
            //spriteBatch.End();
        }
        public void Draw(GameTime gameTime)
        {
            //((MapEdit)this.Game).spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            //m_Map.Draw(((MapEdit)this.Game).graphics, gameTime, ((MapEdit)this.Game).spriteBatch, 0, 0, 1);

            //((MapEdit)this.Game).spriteBatch.End();
        }
    }
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MapEle
    {
        public String strName;
        public String strTex;
        public Texture2D tex;
    }
    public class MapEditView : TDBaseScene
    {
        TDMap m_Map;
        Texture2D[] m_Cursor;
        AniCursor   m_AniCursor;
        MainMenu    m_CtrlMenu;
        int         m_nCurTool;
        //TDBuildEleInfoList m_EleInfo;
        Texture2D m_posTex;
        


        public MapEditView(Game game)
            : base(game)
        {
            m_Map = new TDMap();
            m_Cursor = new Texture2D[4];
            m_AniCursor = new AniCursor();
            m_nCurTool = -1;
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        public override void LoadContent(ContentManager Content) 
        {
            m_Map.LoadContent(Content);
            //m_EleInfo = Content.Load<TDBuildEleInfoList>("BuildElement");
            string[] strcursor = {
                                     "handcursor/00000",
                                     "handcursor/00001",
                                     "handcursor/00002",
                                     "handcursor/00003",
                                 };
            m_posTex = Content.Load<Texture2D>("build/100");
            Point hot = new Point(0,0);
            m_AniCursor.SetAniInfo(Content, strcursor, hot);
            Rectangle rtt = new Rectangle(2,2,32,32);
            m_CtrlMenu = new MainMenu(Game);
            m_CtrlMenu.SetOverlayerMode(true);
            for (int i = 0; i < m_Map.m_EleInfo.BuildEleList.Count; i++)
            {
                m_CtrlMenu.AddMenuItem(m_Map.m_EleInfo.BuildEleList[i].name, m_Map.m_EleInfo.BuildEleList[i].GetTexture(), rtt);
                rtt.Offset(34, 0);
            }
            //m_CtrlMenu.AddMenuItem("004", "build/004", rtt);
            //rtt.Offset(34, 0);
            //m_CtrlMenu.AddMenuItem("005", "build/005", rtt);
            //rtt.Offset(34, 0);
            //m_CtrlMenu.AddMenuItem("006", "build/006", rtt);
            //rtt.Offset(34, 0);
            //m_CtrlMenu.AddMenuItem("007", "build/007", rtt);
            //rtt.Offset(34, 0);
            //m_CtrlMenu.AddMenuItem("008", "build/008", rtt);
            m_CtrlMenu.SetSelIcon("SelIconMask");
            m_CtrlMenu.LoadContent(Content);
            //m_Cursor[0] = Content.Load<Texture2D>("handcursor/00000.tga");
            //m_Cursor[1] = Content.Load<Texture2D>("handcursor/00001.tga");
            //m_Cursor[2] = Content.Load<Texture2D>("handcursor/00002.tga");
            //m_Cursor[3] = Content.Load<Texture2D>("handcursor/00003.tga");
        }

        public override int Update(GraphicsDeviceManager graphics, GameTime gameTime) 
        {
            base.Update(gameTime);
            MouseState mstate = Mouse.GetState();
            
            int nRet = m_CtrlMenu.Update(graphics, gameTime);

            if(nRet >= 0)
            {
               m_nCurTool = nRet;
            }
            if (nRet <= -2)
            {
                if (m_nCurTool >= 0 && mstate.LeftButton == ButtonState.Pressed)
                {
                    int mapy = 0,mapx = 0;
                    m_Map.ScreenToMap(ref mapx, ref mapy, mstate.X, mstate.Y);
                    if (mapy >= 0 && mapx >= 0)
                    {
                        m_Map.AddBuild(m_nCurTool, mapx, mapy);
                    }
                }
                m_Map.Update(gameTime, 0, 0);
                if (mstate.RightButton == ButtonState.Pressed)
                {
                    m_nCurTool = -1;
                }
            }
            m_AniCursor.Update(graphics, gameTime);
            


            return 1;
        }

        public override void Update(GameTime gameTime)
        {
            m_Map.Update(gameTime, 0, 0);
            base.Update(gameTime);
        }
        public override void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int xOffset, int yOffset,float fscale)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            m_Map.Draw(graphics, gameTime, spriteBatch, 0, 0, 1);
            m_CtrlMenu.Draw(graphics, gameTime, spriteBatch, 0, 0, 1);
            MouseState mstate = Mouse.GetState();
            
            if (m_nCurTool >= 0)
            {
                Rectangle rtDes = new Rectangle();
                Texture2D tempTex = m_Map.m_EleInfo.BuildEleList[m_nCurTool].GetTexture();

                for (int i = 0; i < m_Map.m_EleInfo.BuildEleList[m_nCurTool].posOffset.Count; i++)
                {
                    rtDes.X = mstate.X - m_posTex.Width / 2 + m_Map.m_EleInfo.BuildEleList[m_nCurTool].posOffset[i].X * m_posTex.Width / 2;
                    rtDes.Y = mstate.Y - m_posTex.Height / 2 + m_Map.m_EleInfo.BuildEleList[m_nCurTool].posOffset[i].Y * m_posTex.Height / 2;
                    rtDes.Width = m_posTex.Width;
                    rtDes.Height = m_posTex.Height;
                    spriteBatch.Draw(m_posTex, rtDes, Color.White);
                }



                rtDes.X = mstate.X - m_Map.m_EleInfo.BuildEleList[m_nCurTool].centerx;
                rtDes.Y = mstate.Y - m_Map.m_EleInfo.BuildEleList[m_nCurTool].centery;
                rtDes.Width = tempTex.Width;
                rtDes.Height = tempTex.Height;
                Color c = new Color(255, 255, 255, 225);
                spriteBatch.Draw(tempTex, rtDes, c);
                

                
            }
            m_AniCursor.Draw(graphics, gameTime, spriteBatch, mstate.X, mstate.Y);
            spriteBatch.End();
        }

        public void Draw(GameTime gameTime)
        {
            ((MapEdit)this.Game).spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            m_Map.Draw(((MapEdit)this.Game).graphics, gameTime, ((MapEdit)this.Game).spriteBatch, 0, 0, 1);
            m_CtrlMenu.Draw(((MapEdit)this.Game).graphics, gameTime, ((MapEdit)this.Game).spriteBatch, 0, 0, 1);
            
            ((MapEdit)this.Game).spriteBatch.End();
        }
    }
}
